
import CryptoJS from 'crypto-js';
// 正式
//var baseURL1 = 'https://api.hnhr66.cn/';
//var baseURL2 = 'https://admins-api.hnhr66.cn/';


// 正式测试
//var baseURL1 = 'http://apitest.hnhr66.cn/';


var httpURL='https://rh.wexiang.vip:11243'

 // if (process.env.NODE_ENV === 'development') {}

	
function generateHmacSha1(message, secretKey) {
    return CryptoJS.HmacSHA1(message, secretKey).toString(CryptoJS.enc.Hex);
}

var getSignature = () => {
	let user = uni.getStorageSync('user')

	return new Promise((resolve, reject) => {
		uni.request({
			url: baseURL1 + 'getSignature',
			header: {
				'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : '',
				'APP': new Date().getTime()
			},
			method: 'GET',
			success: (res) => {
				if (res.statusCode == 200) {
					if (res.data.code == 200) {
						
						let str = user.appKey+res.data.data.msgId+res.data.data.requestTime
						let upperStr = str.toUpperCase()
						
						res.data.data.signature = generateHmacSha1(upperStr,user.secretKey)
						
						res.data.data.appCode=user.appCode
						res.data.data.appKey=user.appKey
						
						resolve(res.data.data)
						
					}
				} else {
					reject(res)
					if (res.message) {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					} else {
						uni.showToast({
							title: res.data.message,
							icon: "none"
						})
					}
				}
			}
		})
	})
}
 
import w_md5 from '/js_sdk/zww-md5/w_md5.js'

function geturl(urls){
	let url
	let turl = uni.getStorageSync('thaturl')
	if(turl.fuwuselect==1){
		url='http://'
	}else{
		url='https://'
	}
	
	if (process.env.NODE_ENV === 'development') {
		 url+=turl.yumingvalue+':'+turl.duankovalue+urls
	} else {
		url = httpURL+'/server'+urls
	}
	return url
}

export function request(app) {
	
	app.config.globalProperties.geturl=geturl
	console.log(geturl(''),"vvvvv")
	
	app.config.globalProperties.http = (urls, data, method) => {
			
		return new Promise(function(resolve, reject) {
			uni.request({
				url:geturl(urls),
				header: {
					'Authorization':'Bearer '+ (uni.getStorageSync("token") ? uni.getStorageSync("token") : ''),
					'APP': '5152'
				},
				method: method ? method : 'GET',
				data: data ? data : {},
				success: (res) => {
					if (res.statusCode == 200) {
						if (res.data.code === 200) {
							resolve(res.data);
						} else if (res.data.code === 401) {
							uni.removeStorageSync('token')
							uni.removeStorageSync('info')
							uni.removeStorageSync('user')
							uni.showToast({
								title: '登录已过期，请重新登录',
								icon: "none"
							})
							setTimeout(()=>{
								uni.reLaunch({
									url: "/pages/index/login"
								})
							},500)
							
							if(window.loginMgr){
								loginMgr.logout('手动').then(res=>{
									console.log(res,"mmm")
								})
							}
							
						} else {
							uni.showToast({
								title: res.data.msg || res.data.message,
								icon: "none"
							})
							reject(res.data);
						}
					} else {
						reject(res)
						if (res.message) {
							uni.showToast({
								title: res.message,
								icon: "none"
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: "none"
							})
						}
					}
				},
				complete() {
				},
				fail: (res) => {
					reject(res)
					uni.showToast({
						title: "系统频繁，稍后重试",
						icon: "none"
					})
				}
			})
		})
	}
	
	 
	app.config.globalProperties.nhttp = (urls, data, method, baseurl, isload) => {
		
		 let user = uni.getStorageSync('user')
		 var randdtm = Math.floor(new Date().getTime() / 1000)
		 var str = user.devid + ',' + user.devToken + ',' + randdtm + ','+urls
		 var md5token = w_md5.hex_md5_32(str)
		 
		 
		
		return new Promise((resolve, reject) => {
			uni.request({
				url: 'https://121.89.195.116'+urls,
				header: {
					'devid': user.devid,
					'randdtm': randdtm,
					'md5token':md5token
				},
				method: method ? method : 'GET',
				data: data ? data : {},
				success: (res) => {
					if (res.statusCode == 200) {
						
					}
				}
			})
		})
	}
	

	app.config.globalProperties.ghttp = (urls, data, method,manager) => {
		let user = uni.getStorageSync('user')
		var randdtm = Math.floor(new Date().getTime() / 1000)
		
		console.log(urls, data, method,manager)
		if(manager){
			var str = user.devid + ',' + user.devToken + ',' + randdtm + ',/rest/v1/app1/manager/sessions/'+uni.getStorageSync("sdkinfo").session_id+urls
		}else{
			var str = user.devid + ',' + user.devToken + ',' + randdtm + ',/rest/v1/app1/user/sessions/'+uni.getStorageSync("sdkinfo").session_id+urls
		}
		
		var md5token = w_md5.hex_md5_32(str)
	
		console.log(str)
		var url
		
		
		if (process.env.NODE_ENV === 'development') {
			if(manager){
				url = 'https://121.89.195.116/rest/v1/app1/manager/sessions/' + uni.getStorageSync("sdkinfo").session_id + urls
			}else{
				url = '/bsapi/rest/v1/app1/user/sessions/' +uni.getStorageSync("sdkinfo").session_id + urls
			}
		} else {
			console.log('生产环境');
			
			if(manager){
				url = httpURL+'/meeting/rest/v1/app1/manager/sessions/' +uni.getStorageSync("sdkinfo").session_id + urls
			}else{
				url = httpURL+'/meeting/rest/v1/app1/user/sessions/' +uni.getStorageSync("sdkinfo").session_id + urls
			}
			
		}
		// https://039098045179.51vmr.com/rest/v1/app1/manager/sessions/140083016899392/schedules/schedule_ids/search/
		// https://121.89.195.116/rest/v1/app1/manager/sessions/140083039841648/schedules/schedule_ids/search/
		
		
		console.log(url)

		return new Promise((resolve, reject) => {
			uni.request({
				url,
				header: {
					'devid': user.devid,
					'randdtm': randdtm,
					'md5token': md5token
				},
				method: method ? method : 'GET',
				data: data ? data : {},
				success: (res) => {
					if (res.statusCode == 200) {
						resolve(res.data);
					} else if (res.statusCode == 401) {
						
						// uni.removeStorageSync('token')
						// uni.removeStorageSync('user')
						// uni.removeStorageSync('info')
						// uni.showToast({
						// 	title: '登录已过期，请重新登录',
						// 	icon: "none"
						// })
						// loginMgr.logout('手动').then(res=>{
						// 	console.log(res,"mmm")
						// })
						// uni.reLaunch({
						// 	url: "/pages/index/index"
						// })

						reject(res)
					} else {
						reject(res)
						if (res.data.result) {
							uni.showToast({
								title: res.data.result,
								icon: "none"
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: "none"
							})
						}
					}
				},
				fail: (res) => {
					console.log(res)
					reject(res)
					uni.showToast({
						title: "系统频繁，稍后重试",
						icon: "none"
					})
				}
			})

		})
	}
	
	app.config.globalProperties.khttp = (urls, data, method, isallurl) => {
		 let user = uni.getStorageSync('user')
		  var randdtm = Math.floor(new Date().getTime() / 1000)
		  var str = user.devid + ',' + user.devToken + ',' + randdtm + ','+urls
		  var md5token = w_md5.hex_md5_32(str)
		 
		 return new Promise((resolve, reject) => {
		 	uni.request({
		 		url: 'https://121.89.195.116/rest/v1/meetings/temp-verify-code',
		 		header: {
		 			'devid': user.devid,
		 			'randdtm': randdtm,
		 			'md5token':md5token
		 		},
				method: method ? method : 'GET',
				data: data ? data : {},
		 		success: (res) => {
		 			if (res.statusCode == 200) {
						if(res.data){
							resolve(res.data)
						}
		 			} else {
		 				reject(res)
		 				if (res.message) {
		 					uni.showToast({
		 						title: res.message,
		 						icon: "none"
		 					})
		 				} else {
		 					uni.showToast({
		 						title: res.data.message,
		 						icon: "none"
		 					})
		 				}
		 			}
		 		}
		 	})
		 })
	}
	



	// app.config.globalProperties.jhttp = (urls, data, method, baseurl, isload) => {
	// 	console.log(data)
	// 	let url=''
	// 	if (process.env.NODE_ENV === 'development') {
	// 		url = 'http://121.89.195.116:12381/' + urls
	// 	} else {
	// 		url = 'https://lov.wexiang.vip/meeting' + '/rest/v1/app1/user/sessions/' + user.meetingToken + urls
	// 	}
		
	// 	return new Promise((resolve, reject) => {
	// 		getSignature().then(res=>{
				
	// 			// 原始字符串
	// 			let str = "dqyyfbptyg:1234";
				 
	// 			// 使用btoa()进行Base64编码
	// 			let encodedStr = btoa(str);
				
				
	// 		uni.request({
	// 			url,
	// 			header: {
	// 				 'Authorization':'Basic '+encodedStr,
					
	// 				 'Content-Type': 'application/json',
	// 				 'Content-Length':"",
	// 				 'X-ServerCode':"authentication-token",
	// 				 'X-MsgId':res.msgId,
	// 				 'X-RequestTime':res.requestTime,
	// 				 'X-AppCode':res.appCode,
	// 				 'X-AppKey':res.appKey,
	// 				 'X-Signature':res.signature
	// 			},
	// 			method: method ? method : 'GET',
	// 			data: data ? data : {},
	// 			success: (res) => {
	// 				if (res.statusCode == 200) {
	// 					resolve(res.data);
	// 				} else if (res.statusCode == 401) {
	// 					// uni.removeStorageSync('token')
	// 					// uni.showToast({
	// 					// 	title: '登录已过期，请重新登录',
	// 					// 	icon: "none"
	// 					// })
	// 					// uni.reLaunch({
	// 					// 	url: "/pages/index/index"
	// 					// })

	// 					// reject(res)
	// 				} else {
	// 					reject(res)
	// 					if (res.data.result) {
	// 						uni.showToast({
	// 							title: res.data.result,
	// 							icon: "none"
	// 						})
	// 					} else {
	// 						uni.showToast({
	// 							title: res.data.message,
	// 							icon: "none"
	// 						})
	// 					}
	// 				}
	// 			},
	// 			complete() {
	// 				uni.hideLoading()
	// 			},
	// 			fail: (res) => {
	// 				reject(res)
	// 				uni.showToast({
	// 					title: "系统频繁，稍后重试",
	// 					icon: "none"
	// 				})
	// 			}
	// 		})
	// 		})


	// 	})
	// }


}